package com.jiangnan.ui.activity

import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.widget.ImageView
import android.widget.TextView
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.databinding.DataBindingUtil
import androidx.fragment.app.Fragment
import androidx.viewpager2.widget.ViewPager2
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.jiangnan.R
import com.jiangnan.databinding.ActivitySlideNavigationBinding
import com.jiangnan.entity.LoginInfo
import com.jiangnan.ui.adapter.SlideNavigationAdapter
import com.jiangnan.ui.fragment.MyFragment
import com.jiangnan.ui.fragment.RecoFragment
import com.jiangnan.ui.fragment.RecommendFragment
import com.jiangnan.ui.fragment.RespositoryFragment
import com.jiangnan.ui.fragment.ReturnFragment
import com.jiangnan.util.CurrentLoginInfo
import com.jiangnan.util.SharedPrefUtil


/**
 * ViewPager2+Fragment+TabLayout实现多页面滑动
 */
class SlideNavigationActivity : BaseActivity() {
    private val titles = arrayOf(
        "库存",
        "推荐",
        "AI管家",
        "还书",
        "我的"
    )
    private val icons = intArrayOf(
        R.drawable.book_respositort_selector,
        R.drawable.book_recommend_selector,
        R.drawable.book_ai_selector,
        R.drawable.book_return_selector,
        R.drawable.book_me_selector
    )

    private var titleTxt: TextView? = null

    private val fragments: MutableList<Fragment> = ArrayList()

    private var tabLayout: TabLayout? = null

    private var viewPager2: ViewPager2? = null
    private var navigationAdapter: SlideNavigationAdapter? = null

    private val tabviews: MutableList<View> = ArrayList()

    private var pageChangeCallback: PageChangeCallback? = null

    private lateinit var binding: ActivitySlideNavigationBinding

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 使用 DataBinding 初始化布局
        binding = DataBindingUtil.setContentView(this, R.layout.activity_slide_navigation)
        binding.lifecycleOwner = this
        initFragment()
        initView()
        var loginInfo = SharedPrefUtil.getLoginInfo(this)
        CurrentLoginInfo.setCurrentLoginInfo(loginInfo!!)
    }

    override fun onSaveInstanceState(outState: Bundle) {
        super.onSaveInstanceState(outState)
    }

    /**
     * 初始化Fragment
     */
    private fun initFragment() {
        fragments.add(RespositoryFragment())
        fragments.add(RecoFragment())
        fragments.add(RecommendFragment())
        fragments.add(ReturnFragment())
        fragments.add(MyFragment())
    }


    /**
     * 初始化view
     */
    private fun initView() {
        titleTxt = binding.acNavigationTitle

        tabLayout = binding.acNavigationTab

        for (i in 0..4) {
            val v = LayoutInflater.from(this).inflate(R.layout.custom_tab_item, null)
            val img = v.findViewById<ImageView>(R.id.tv_img)
            val tv = v.findViewById<TextView>(R.id.tab_tv)
            tv.text = titles[i]
            img.setBackgroundResource(icons[i])
            tabLayout?.newTab()?.let { tabLayout?.addTab(it.setCustomView(v)) }
            tabviews.add(v)
        }

        viewPager2 = findViewById<ViewPager2>(R.id.ac_slidenavigation_pager)
        pageChangeCallback = PageChangeCallback()
        viewPager2?.registerOnPageChangeCallback(pageChangeCallback!!)

        navigationAdapter = SlideNavigationAdapter(
            supportFragmentManager, lifecycle, fragments
        )
        viewPager2?.setAdapter(navigationAdapter)

        /**
         * 这部分逻辑用于将 TabLayout 与 ViewPager2 的联动
         */
        val mediator = TabLayoutMediator(tabLayout!!, viewPager2!!) { tab: TabLayout.Tab, position: Int ->
            tab.setCustomView(
                tabviews[position]
            )
        }
        mediator.attach()

//        val mediator = TabLayoutMediator(tabLayout!!, viewPager2!!,
    //        object : TabLayoutMediator.TabConfigurationStrategy {
//            override fun onConfigureTab(tab: TabLayout.Tab, position: Int) {
//                tab.setCustomView(tabviews[position])
//            }
//        })
//        mediator.attach()
    }

    override fun onDestroy() {
        super.onDestroy()
        viewPager2!!.unregisterOnPageChangeCallback(pageChangeCallback!!)
    }

    private inner class PageChangeCallback : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrolled(
            position: Int, positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            Log.i(
                TAG, "position=" + position + " ,positionOffset=" + positionOffset +
                        " ,positionOffsetPixels=" + positionOffsetPixels
            )
        }

        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            Log.i(TAG, "position=$position")
            titleTxt!!.text = titles[position]
        }

        override fun onPageScrollStateChanged(state: Int) {
            super.onPageScrollStateChanged(state)
            Log.i(TAG, "state=$state")
        }
    }


    companion object {
        private const val TAG = "SlideNavigationActivity"
    }
}